declare let eManager;
import DrawOne from "./DrawOne";
const { ccclass, property } = cc._decorator;
//5-1 眼影
@ccclass
export default class YanYing extends cc.Component {
    @property
    stepName = "";
    //刷子入场动画 （右眼1 左眼2）
    @property(cc.Animation)
    startAni: cc.Animation = null;
    //左眼 默认闭眼 
    @property(cc.Node)
    lefteyeClose: cc.Node = null;
    //右眼 默认闭眼 
    @property(cc.Node)
    righteyeClose: cc.Node = null;
    //右眼 效果闭眼 
    @property(cc.Node)
    righteyeCloseEff: cc.Node = null;
    //左眼 涂抹虚线区域 
    @property(cc.Node)
    leftDrawArea: cc.Node = null;
    //右眼 涂抹虚线区域 
    @property(cc.Node)
    rightDrawArea: cc.Node = null;

    @property(DrawOne)
    leftDraw: DrawOne = null;
    @property(DrawOne)
    rightDraw: DrawOne = null;

    //音效
    @property({type:cc.AudioClip})
    sound: cc.AudioClip = null;

    //根节点 动画
    @property(cc.Animation)
    rootAni:cc.Animation = null;

    _isPlay = false;
    _isDone = false;
    _curPoint = 1;//一共2步  先是右眼 再是左眼
    _audioId = 0;
    start() {
        //@ts-ignore
        this.startAni.on("finished", this.startFinish, this);
        //@ts-ignore
        this.rootAni.on("finished",this.rootFinish,this);
    };
    initData() {
        // eManager.emit("HZ_SHOW_SELECT_LIST", {
        //     step: 5,
        //     point: 1,
        //     // callback: () => {
        //     //     this.rootAni.play("step4_1_01");
        //     // }
        // });
        this.rootAni.play("step4_1_01");
        this._isPlay = true;
        this._audioId = 0;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_5_1");
    };

    _touchStart(event) {
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        if (this._audioId > 0) {
            cc.audioEngine.stop(this._audioId);
        }
        if (this._curPoint == 1) {
            this.rightDraw._touchStart(event,cc.v2(-70,-80));
            this._audioId =  cc.audioEngine.play(this.sound, true, 1);
        } else if (this._curPoint == 2) {
            this.leftDraw._touchStart(event,cc.v2(-70,-80));
            this._audioId =  cc.audioEngine.play(this.sound, true, 1);
        }
    };
    _touchMove(event) {
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        if (this._curPoint == 1) {
            this.rightDraw._touchMove(event);
        } else if (this._curPoint == 2) {
            this.leftDraw._touchMove(event);
        }
    };

    _touchEnd(event: any) {
        cc.audioEngine.stop(this._audioId);
        this._audioId = 0;
        if (this._isPlay)
            return;
        if (this._isDone)
            return;
        if (this._curPoint == 1) {
            this.rightDraw._touchEnd(() => {
                this.lefteyeClose.active = false;//睁眼
                this.righteyeClose.active = false;
                this.rightDrawArea.active = false;
                this.rootAni.play("step4_1_02");
                this._isPlay = true;
            });
        } else if (this._curPoint == 2) {
            this.leftDraw._touchEnd(() => {
                this.leftDrawArea.active = false;
                this.lefteyeClose.active = false;
                this.righteyeCloseEff.active = false;
                this._isDone = true;
                this.startAni.node.active = false;
                this.rootAni.play("step4_1_03");
                eManager.emit("DONE_POINT", 1);
                eManager.emit("MINI_DONE_POINT", 2);
                eManager.emit("HZ_MAKE_DOT", {
                    makeup_close:5,
                    makeup_step:this.stepName
                });
            });
        }
    };

    startFinish(event: cc.Event.EventCustom, animState: cc.AnimationState) {
        if (animState.name == "step5_1yanyingshua_chuxian_01") {
            this.lefteyeClose.active = true;//闭眼
            this.righteyeClose.active = true;
            this.rightDrawArea.active = true;
            this._isPlay = false;
        } else if (animState.name == "step5_1yanyingshua_chuxian_02") {
            this._curPoint++;
            this.leftDrawArea.active = true;
            this.lefteyeClose.active = true;//左眼默认闭眼
            this.righteyeCloseEff.active = true;//右眼效果闭眼
            this._isPlay = false;
        }
    };

    rootFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if(animState.name == "step4_1_01"){
            this.startAni.node.active = true;
            this.startAni.play("step5_1yanyingshua_chuxian_01");//右眼 刷子进入动画
        }
        else if(animState.name == "step4_1_02"){
            this.startAni.play("step5_1yanyingshua_chuxian_02");
        }
    };

}
